<!-- 首页 -->
<template>
	<view class="page_box">
		<view class="page">
			<!-- 空白页 -->
			<view class="page_box shopro-selector">
				<!-- 导航栏 -->
				<view class="head_box" :style="{ background: bgcolor }">
					<cu-custom :isBack="true" :showBackIcon="false">
						<block slot="backText">
							<text class="nav-title shopro-selector-rect">首页</text>
						</block>
					</cu-custom>
				</view>
				<view class="content_box" style="margin-top: -4rpx;overflow: hidden;">
					<scroll-view @refresherrefresh="refresh" :refresher-triggered="isRefreshing"
						:refresher-enabled="true" class="scroll-box" scroll-y scroll-with-animation enable-back-to-top>
						<view class="scroll_content">
							<view class="progress_box y-center">
								<view class="head shsc">当前成事节点</view>
								<view class="num shsc w100 fb df-c"><text class="mr30">{{nodegameMessage.num}}</text></view>
								<view class="progress_line">
									<progress :daygameMessage="daygameMessage" :max="nodegameMessage.node[2].num"
										:min="nodegameMessage.node[0].num" :node="nodegameMessage.node"
										:currNum="nodegameMessage.num"></progress>
								</view>
								<view :style="{opacity:nodegameStatus?1:0.5}" class="join_btn x-center fb"
									@click="showJoin(nodegameStatus)">
									<text v-if="button">{{button}}</text>
								</view>
								<view v-if="nodegameStatus" class="game_info fs28 mt30">仅剩<span
										class="mx10">{{nodegameMessage.lack}}</span>人即可共享下轮大奖</view>
								<!--  规则说明  -->
								<view
									@click="jump('/pages/user/rules/rules',{type:'rich',richText:nodegameMessage.rule})"
									class="rules fs28 df-c">
									规则说明
								</view>
							</view>
							<view class="award y-f">
								<view class="award_list df-y">
									<view class="award_head shsc x-center">
										<span>10万元</span>大奖等你拿
									</view>
									<swiper class="wh100" :indicator-dots="true" :interval="3000" :duration="1000">
										<swiper-item v-for="(item1,index1) in awardListPages" :key="index1">
											<view class="lists">
												<view class="list y-f" v-for="(item2,index2) in awardList[index1]"
													:key="index2">
													<view class="img">
														<image style="height: 110rpx;" :src="IMGURL+item2.image"
															mode="heightFix"></image>
													</view>
													<view style="text-align: center;" class="title mt20 one-t">
														{{item2.name}}
													</view>
												</view>
											</view>
										</swiper-item>
									</swiper>
									<view class="bottom df-c">- 100,000 bonus -</view>
								</view>
							</view>
							<view class="game_status bs px30">
								<gameRules v-if="!nodegameJoined" :rule="rules"></gameRules>
								<alreadyJoin v-if="nodegameJoined"></alreadyJoin>
							</view>
							<view class="wish_bottom df-c shsc">
								- 愿每一杯好茶，都相伴成事 -
							</view>
						</view>
					</scroll-view>
				</view>
				<!-- 自定义底部导航 -->
				<shopro-tabbar></shopro-tabbar>
				<!-- 每日競速 -->
				<view @click="jump('pages/public/day-game/day-game')" v-if="daygameStatus" class="rate_everyDay">
					<image style="width:111rpx ;height:111rpx ;" :src="$IMG_URL+daygameMessage.image" mode="widthFix">
					</image>
				</view>
				<!-- 加入节点赛弹出层 -->

				<!-- 不是成事节点 {未登录} -->
				<u-popup mode="bottom" border-radius="30" height="450rpx" :custom-style="popupStyle" v-model="show1"
					:safe-area-inset-bottom="true">
					<view class="popup bs df-yc">
						<view class="info fb fs36 df-yc">
							<text class="my10">成事节点赛仅<text class="mx10 highlight">成事节点</text>用户可以报名</text>
							<text style="font-weight: 300;" class="my10">是否成为成事节点？</text>
						</view>
						<view class="buttons w100 bs fb df-xb">
							<view class="cancel button df-c" @click="show1=false">取消</view>
							<view @click="login" class="confirm button df-c">成为成事节点</view>
						</view>
					</view>
				</u-popup>
				<!-- 不是成事节点 {未邀请三位好友} -->
				<u-popup mode="bottom" border-radius="30" height="450rpx" :custom-style="popupStyle" v-model="show2"
					:safe-area-inset-bottom="true">
					<view class="popup bs df-yc">
						<view class="info fb fs36 df-yc">
							<text class="my10">无法加入当前成事赛,请先<text class="mx10 highlight">邀请三名好友</text></text>
							<text style="font-weight: 300;opacity: 0.8;" class="my10">目前已邀请<text
									class="mx10 highlight">{{noQualiInfo.child_user_num}}</text>名好友,还差<text
									class="mx10 highlight">{{noQualiInfo.need_num-noQualiInfo.child_user_num}}</text>名</text>
						</view>
						<view class="buttons w100 bs fb df-xb">
							<view class="cancel button df-c" @click="show2=false">取消</view>
							<view @tap="jump('/pages/public/poster/index',{posterType:'seal',shareType:'node_game'})"
								class="confirm button df-c">前往邀请</view>
						</view>
					</view>
				</u-popup>

				<!-- 已經是成事节点 -->
				<u-popup mode="bottom" border-radius="30" height="450rpx" :custom-style="popupStyle" v-model="show3"
					:safe-area-inset-bottom="true">
					<view class="popup bs df-yc">
						<view class="info fb fs36 df-yc">
							<text class="my10">已经是<text class="mx10 highlight">成事节点</text>用户</text>
							<text style="font-weight: 300;" class="my10">是否报名加入当前节点赛？</text>
						</view>
						<view class="buttons w100 bs fb df-xb">
							<view class="cancel button df-c" @click="show3=false">取消</view>
							<view @click="joinHandler" class="confirm button df-c">加入当前节点</view>
						</view>
					</view>
				</u-popup>


				<!-- 空投弹出层 -->
				<popup @get_prize_success="getPrizeSuccess" v-if="ktShow==20" :ktList="ktList" @close="closeKt"
					:getData="getData"></popup>
				<!-- 登录 -->
				<shopro-login-modal></shopro-login-modal>
				<!-- 领到的奖品 -->
				<getPrize ref="getPrize"></getPrize>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapActions,
		mapState
	} from 'vuex';
	import progress from "./components/progress.vue"
	import gameRules from "./components/gameRules.vue"
	import alreadyJoin from "./components/alreadyJoin.vue"
	import popup from "./components/popup.vue"
	import getPrize from './components/get-prize.vue'

	export default {
		components: {
			progress,
			gameRules,
			alreadyJoin,
			popup,
			getPrize
		},
		data() {
			return {
				bgcolor: '#622128',
				show1: false,
				show2: false,
				show3: false,
				ktShow: false,
				IMGURL: this.$IMG_URL,
				isRefreshing: false,
				popupStyle: {

				},
				floatBtnSize: {
					width: "150rpx",
					height: "150rpx"
				},
				awardList: [],
				awardListPages: 0,
				rules: "",
				nodegameMessage: {},
				daygameMessage: {},
				//按钮显示文字
				buttonText: {
					0: {
						0: '比赛还未开启'
					},
					1: {
						0: '加入当前节点赛',
						1: '立即助力'
					}
				},
				//没有参赛资格的提示信息
				noQualiInfo: {},
				ktList: [],
				getData: {},
				firstEnter: true
			};
		},
		computed: {
			daygameStatus() { //竞速赛是否开启
				return this.$store.state.user.dayGameStatus
			},
			nodegameStatus() { //节点赛是否开启
				return this.$store.state.user.nodegameStatus
			},
			nodegameJoined() { //是否参加了节点赛
				return this.$store.state.user.joinNodeGame
			},
			button() { //不同状态下的按钮提示信息
				let text = this.nodegameStatus ? this.buttonText[this.nodegameStatus][this.nodegameJoined] : this
					.buttonText[this.nodegameStatus][0]
				return text
			},
			isLogin() { //是否登录
				return Boolean(uni.getStorageSync('token'))
			}
		},
		onPullDownRefresh() {
			this.init();
		},
		onLoad(options) {
			let that = this;
			uni.getStorage({
				key: 'result_time',
				success: function(res) {
					let now = that.$tools.timestamp_symbol(new Date(), '')
					if (now > res.data) {
						console.log("刷新一次性弹窗状态")
						uni.setStorageSync('result_pop', true)
					}
				},
				fail: function() {
					uni.setStorageSync('result_pop', true)
				}
			})
			// #ifdef APP-VUE
			if (!plus.runtime.isAgreePrivacy()) {
				this.showPrivacy = true;
				this.showNoticeModal = false;
			}
			// 监听设备网络状态变化事件，接口ios市场首次安装网络切换问题
			plus.globalEvent.addEventListener('netchange', function() {
				var nt = plus.networkinfo.getCurrentType(); //网络状态
				if (nt !== plus.networkinfo.CONNECTION_UNKNOW && nt !== plus.networkinfo.CONNECTION_NONE) {
					that.init();
				}
			});
			// #endif
			this.getGameIndex()
			this.getDayGameStatus()
		},
		created() {

		},
		mounted() {

		},
		onShow() {

		},
		methods: {
			...mapActions(['getDayGameStatus']),
			login() {
				this.$store.commit('LOGIN_TIP', true);
			},
			refresh() {
				this.getGameIndex()
			},
			closeKt() {
				this.ktShow = false
			},
			...mapActions(['getAppInit', 'getTemplate']),
			// 初始化
			init() {
				return Promise.all([this.getAppInit(), this.getTemplate()]).then(() => {
					uni.stopPullDownRefresh();
				});
			},
			getPrizeSuccess(data) {
				this.$refs.getPrize.open(data)
			},

			// 点击无网络按钮
			onEmptyBtn() {
				this.init();
			},


			// 路由跳转
			jump(path, params) {
				this.$Router.push({
					path: path,
					query: params
				});
			},
			//节点赛信息
			getGameIndex() {
				if (!this.firstEnter) {
					if (!this.isRefreshing) {
						this.isRefreshing = true;
					}
				}
				this.$api('nodeGame.gameIndex').then((res) => {
					const that = this
					this.firstEnter = false
					setTimeout(e => {
						that.isRefreshing = false;
					})
					let pages = parseInt(res.data.prizes.length / 8)
					let rem = res.data.prizes.length % 8 ? 1 : 0
					this.$store.commit('DAY_GAME_STATUS', res.data.day_game.status)
					this.$store.commit('NODE_GAME_JOIN', res.data.game.join)
					this.$store.commit('NODE_GAME_STATUS', res.data.game.status)
					this.$store.commit('RANKING_LIST', res.data.leaderboard)
					this.nodegameMessage = res.data.game
					this.daygameMessage = res.data.day_game
					this.awardListPages = pages + rem
					this.rules = res.data.game.rule
					this.ktShow = res.data.game.prize.prizes_type
					this.ktList = res.data.prizes
					this.getData.game_stage = res.data.game.prize.game_stage
					this.getData.prizes_type = res.data.game.prize.prizes_type
					let arr = []
					let start = 0
					for (let i = start; i < (pages + rem); i++) {
						if (i == (pages + rem - 1)) {
							arr.push(res.data.prizes.slice(start))
						}
						arr.push(res.data.prizes.slice(start, 8))
						start = start + 8
					}
					this.awardList = arr
				})
			},
			//加入比赛弹窗
			async showJoin(status) {
				let agree = await this.checkJoin() //判断报名资格
				if (!this.isLogin) {
					this.show1 = true
					return
				}
				if (!status) {
					this.$tools.toast("比赛还未开启")
					return
				}
				if (status) {
					switch (agree) {
						case 1:
							if (!this.nodegameJoined) {
								this.show3 = true
							} else if (this.nodegameJoined) {
								this.jump('/pages/public/poster/index', {
									posterType: 'seal',
									shareType: 'node_game'
								})
							}
							break;
						case 0:
							this.show2 = true
							break;
					}
				}
			},
			// 查询参赛资格
			checkJoin() {
				return new Promise((resolve, reject) => {
					this.$api('nodeGame.joinGame').then(res => {
						if (res.code == 1) {
							resolve(1)
						} else {
							resolve(0)
							this.noQualiInfo = res.data
						}

					}).catch(err => {
						this.noQualiInfo = err.data.data
						resolve(0)
					})
				})
			},
			// 加入成事节点赛
			joinHandler() {
				this.show1 = false
				this.$api('nodeGame.confirmJoin').then(res => {
					this.show3 = false
					this.getGameIndex()
				}).catch(err => {
					console.log(err)
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.page_box {
		background-color: #622128;
		height: 100vh;
		width: 100vw;

		.head_box {
			width: 100%;
		}
	}

	.float_btns {
		position: fixed;
		right: 30rpx;
		bottom: 270rpx;
		z-index: 9998;
		width: 150rpx;
	}

	.rate_everyDay {
		position: fixed;
		top: 180rpx;
		left: 20rpx;
		z-index: 9999;
	}

	.wish_bottom {
		font-size: 24rpx;
		font-weight: 500;
		color: #E3AB6E;
		margin-top: 60rpx;
	}

	.popup {
		padding-top: 80rpx;

		.buttons {
			padding: 0 60rpx;
			margin-top: 70rpx;

			.button {
				width: 300rpx;
				height: 88rpx;
				border-radius: 44rpx;

			}

			.cancel {
				color: #999999;
				background: #EFEFEF;
			}

			.confirm {
				color: #FFFFFF;
				background: linear-gradient(270deg, #53181E, #7D333B);
				box-shadow: 0px 5rpx 18rpx 0rpx rgba(83, 24, 30, 0.35);
			}
		}
	}

	.head_box {
		.nav-title {
			font-size: 32rpx;
			font-family: PingFang SC;
			color: #FFFFFF;
		}
	}

	.content_box {
		color: #FFFFFF;
		width: 750rpx;

		.scroll_content {
			padding-bottom: 140rpx;

		}


		.progress_box {
			padding-top: 150rpx;
			background-image: url(#{$IMG_URL}/bg.png);
			background-size: 750rpx;
			background-repeat: no-repeat;
			position: relative;

			.rules {
				position: absolute;
				right: 0;
				top: 80rpx;
				width: 166rpx;
				color: #FFFFFF;
				height: 56rpx;
				border-radius: 28rpx 0px 0px 28rpx;
				background: #581E24;
			}

			.progress_line {
				margin-top: 175rpx;
			}

			.head {
				font-size: 28rpx;
				font-weight: 300;
				opacity: 0.7;
			}

			.num {
				font-size: 90rpx;
				font-weight: bold;
				font-style: italic;
				background: linear-gradient(180deg, #FFFFFF 10%, #E3AB6E 90%);
				-webkit-background-clip: text;
				color: transparent;
				text-shadow: 0px 4px 29px rgba(23, 95, 204, 0.14);
			}

			.join_btn {
				width: 690rpx;
				height: 88rpx;
				background: linear-gradient(270deg, #B18540, #F7D9AA);
				box-shadow: 0px 3rpx 1rpx 0px #580F17;
				border-radius: 44rpx;
				margin-top: 98rpx;
				color: #622128;
			}

			.game_info {
				opacity: 0.7;

				span {
					color: #F7D9AA;
				}
			}
		}

		.award {
			position: relative;
			width: 750rpx;
			height: 580rpx;
			margin-top: 122rpx;

			.award_list {
				width: 690rpx;
				height: 100%;
				background: #53181E;
				border: 1rpx solid #E3AB6E;
				border-radius: 20rpx;
				position: absolute;
				z-index: 50;
				display: flex;
				padding: 120rpx 30rpx 10rpx 30rpx;

				.lists {
					display: flex;
					flex-wrap: wrap;
				}

				.list {
					margin-bottom: 40rpx;
					width: calc(624rpx/4);

					.title {
						font-size: 22rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #FFFFFF;
						width: 150rpx;

					}

					.img {
						width: 110rpx;
						height: 110rpx;
						background-color: #FFFFFF;
						border-radius: 50%;
						overflow: hidden;

					}
				}

				.award_head {
					width: 490rpx;
					position: absolute;
					top: -17rpx;
					left: 50%;
					height: 86rpx;
					background-image: url(#{$IMG_URL}/biaoti.png);
					background-size: 490rpx;
					transform: translate(-50%);
					background-position: center;
					background-repeat: no-repeat;
					font-size: 32rpx;

					span {
						color: #E3AB6E;
					}
				}

				.bottom {
					font-family: "Bodoni MT";
					font-size: 24rpx;
					color: #E3AB6E;
					margin-top: 0rpx;
				}
			}

			.image {
				width: 750rpx;
				position: absolute;

				.img1 {
					width: 100%;
					left: 0;

				}
			}
		}

		.game_status {
			margin-top: 60rpx;
		}

	}
</style>
